<?php

/**
* 好友关系的相关操作
*/
class UCModBlacklist
{
    private $_userid;
    private $_errno;
    private $_error;
    private $_included = false;
    
    public function __construct($userid)
    {
        if( !$this->_included ) {
            require_once(dirname(__FILE__).'/../../../unified/SDOApiBlacklistService.php');
            require_once(dirname(__FILE__).'/../user/UCModUserInfo.php'); 
            $this->_included = true;
        }
        $this->_userid = $userid;
    }
    
    public function changeUserid($userid) {
        $this->_userid = $userid;
    }
    
    private function _isSuccess($ret) {
        $this->_errno = (is_array($ret) && array_key_exists('code', $ret))? $ret['code']:-1;
        $this->_error = (is_array($ret) && array_key_exists('msg', $ret))? $ret['msg']:'';
        return ($this->_errno == 0)? true:false;
    }
    
    private function _addonInfo($ar_entry)
    {
        $ar_user = array();
        if( !is_array($ar_entry) || !array_key_exists('entry', $ar_entry) )
            return $ar_entry;
        foreach($ar_entry['entry'] as $entry)
        {
            $ar_user[] = $entry['value'];
        }
        //$user_list = implode(',', $ar_user);
        $ar_info = UCModUserInfo::getUserInfos($ar_user, array(USERINFO_NICKNAME, USERINFO_AVATAR_30, USERINFO_GENDER));
       
        foreach($ar_entry['entry'] as &$entry)
        {
            $userid = $entry['value'];
            if( empty($ar_info[$userid][USERINFO_NICKNAME]) )
                $ar_info[$userid][USERINFO_NICKNAME] = $userid;
                
			$entry = array( 'sdid' => $userid,
							'nick' => $ar_info[$userid][USERINFO_NICKNAME],
							'avatar' => $ar_info[$userid][USERINFO_AVATAR_30]);
        }
        return $ar_entry;
    }
    
    public function getLastErrno()
    {
        return $this->_errno;
    }
    public function getLastError()
    {
        return $this->_error;
    }
    
    /**
    * 添加一个用户到黑名单
    * 
    * @param long 要拉黑名单的用户id
    * @return false/true
    */
    public function addPersonToBlacklist($target_userid)
    {
        if( empty($target_userid) ) {                                         
            return false;
        }
        $ret = SDOApiBlacklistService::addPersonToBlacklist($this->_userid, $target_userid);
        return $this->_isSuccess($ret); 
    }
    
    /**
    * 查询我的黑名单列表
    * 
    * @return false/Array
    */
    public function getMyBlacklist()
    {
        $ret = SDOApiBlacklistService::getMyBlacklist($this->_userid);
        return !$this->_isSuccess($ret)?false:$this->_addonInfo($ret['data']);
    }
    
    /**
    * 查询我的黑名单列表总数
    * @return false/int
    */
    public function getBlacklistCount()
    {
        $ret = SDOApiBlacklistService::getBlacklistCount($this->_userid);
        return !$this->_isSuccess($ret)? false:intval($ret['data']['totalResults']);
    }
    
    /**
    * 判断$userid是否在我的黑名单中
    * 
    * @param long $userid 
    * @return false/ 0:不在黑名单 / 1: 在黑名单
    */
    public function getJudgePerson($userid)
    {
        if( empty($userid) ) {                                         
            return false;
        }
        $ret = SDOApiBlacklistService::getJudgePerson($this->_userid, $userid);
        return !$this->_isSuccess($ret)?false:$ret['data']['data'];
    }
    
    /**
    * 查询我被谁屏蔽
    * 
    * @return false/Array(
    *  sdid1 => Array(
    *           'nick' => xxx,
    *           'avatar' => xxx,
    *       ),
    *  sdid2 => Array(
    *           'nick' => xxx,
    *           'avatar' => xxx,
    *       ),  
    * )
    */
    public function getWhoShieldMe()
    {
        $ret = SDOApiBlacklistService::getWhoShieldMe($this->_userid);
        if( $this->_isSuccess($ret) === false )
            return false;
        $tmp_ret_ar = $this->_addonInfo($ret['data']);
        //UCLibLogger::debug($tmp_ret_ar);
        $ret_ar = array();
        if( isset($tmp_ret_ar['entry'])) {
            foreach($tmp_ret_ar['entry'] as $tmp_ret) {
                $ret_ar[$tmp_ret['sdid']] = array(
                    'nick' => $tmp_ret['sdid'],
                    'avatar' => $tmp_ret['avatar']
                );
            }
        }
        return $ret_ar;
    }
    
    /**
    * 根把一个用户$target_userid从黑名单中移除
    * 
    * @param long $target_userid   
    * @return false/true
    */
    public function deletePersonOutBlacklist($target_userid)
    {
        if( empty($target_userid) ) {                                         
            return false;
        }
        $ret = SDOApiBlacklistService::deletePersonOutBlacklist($this->_userid, $target_userid); 
        return $this->_isSuccess($ret);
    }
    
    /**
     * 移除黑名单中所有用户
     * @return true/false 
     */
    public function deleteAll()
    {
        $ret = SDOApiBlacklistService::deleteAll($this->_userid);
        return $this->_isSuccess($ret);
    }
}
/*
$o = new UCModBlacklist(1100001308490);
$ret = $o->getMyBlacklist();
var_dump($ret, $o);
*/


